import { checkUserProduct } from '@/api/userinfo/UserInfoApi';
import { AppConsts } from '@/models/AppConsts';
import storage from '@/utils/storage';
import { getAuthUrl } from '@/utils/utils';
import { showToast } from 'vant';
import { createRouter, createWebHashHistory } from 'vue-router'

var routes = [
    { path: "/login", component: () => import('@/views/login/Login.vue'), meta: { anonymous: true, title: "欢迎登录" } },
    { path: "/testlogin", component: () => import('@/views/login/TestLogin.vue'), meta: { anonymous: true, title: "模拟登录" } },
    { path: "/", component: () => import('@/views/index.vue'), meta: { title: "首页" } },
    { path: "/goods/list", component: () => import('@/views/goods/GoodsList.vue'), meta: { title: "商品列表" } },
    { path: "/userinfo/detail", component: () => import('@/views/userinfo/UserInfo.vue'), meta: { title: "用户信息" } },
    { path: "/userinfo/goods", component: () => import('@/views/userinfo/Goods.vue'), meta: { title: "用户产品" } },

    { path: "/plan/make", component: () => import('@/views/execute/GamePlan.vue'), meta: { title: "构建计划" } },
    { path: "/plan/wisdom", component: () => import('@/views/execute/game/Wisdomplus.vue'), meta: { title: "AI计划" } },
    { path: "/plan/auxiliary", component: () => import('@/views/execute/game/Auxiliary.vue'), meta: { title: "辅助工具" } },
    { path: "/plan/auxiliary2.0", component: () => import('@/views/execute/game/AuxiliaryV2.0.vue'), meta: { title: "辅助工具2.0" } },
    { path: "/plan/auxiliary2.1", component: () => import('@/views/execute/game/AuxiliaryV2.1.vue'), meta: { title: "辅助工具2.1" } },
    { path: "/plan/auxiliary3.0", component: () => import('@/views/execute/game/AuxiliaryV3.0.vue'), meta: { title: "辅助工具3.0" } },
    { path: "/plan/boss", component: () => import('@/views/execute/game/BossGame.vue'), meta: { title: "编译计划" } },
    { path: "/plan/boss1.1", component: () => import('@/views/execute/game/BossGameV1.1.vue'), meta: { title: "编译计划1.1" } },
    { path: "/plan/boss1.2", component: () => import('@/views/execute/game/BossGameV1.2.vue'), meta: { title: "编译计划1.2" } },
    { path: "/plan/boss1.3", component: () => import('@/views/execute/game/BossGameV1.3.vue'), meta: { title: "编译计划1.3" } },
    { path: "/plan/boss1.5", component: () => import('@/views/execute/game/BossGameV1.5.vue'), meta: { title: "编译计划1.5" } },
    { path: "/plan/boss2.0", component: () => import('@/views/execute/game/BossGameV2.vue'), meta: { title: "编译计划2.0" } },
    { path: "/plan/boss3.0", component: () => import('@/views/execute/game/BossGameV3.vue'), meta: { title: "编译计划3.0" } },
    { path: "/plan/boss3.0.1", component: () => import('@/views/execute/game/BossGameV3.0.1.vue'), meta: { title: "编译计划3.0.1" } },
    { path: "/plan/boss4.0", component: () => import('@/views/execute/game/BossGameV4.vue'), meta: { title: "编译计划4.0" } },
    { path: "/plan/boss5.0", component: () => import('@/views/execute/game/BossGameV5.vue'), meta: { title: "编译计划5.0" } },
    { path: "/plan/boss5.1", component: () => import('@/views/execute/game/BossGameV5.1.vue'), meta: { title: "编译计划5.1" } },
    { path: "/plan/boss5.2", component: () => import('@/views/execute/game/BossGameV5.2.vue'), meta: { title: "编译计划5.2" } },
    { path: "/plan/boss5.3", component: () => import('@/views/execute/game/BossGameV5.3.vue'), meta: { title: "编译计划5.3" } },
    { path: "/plan/boss5.5", component: () => import('@/views/execute/game/BossGameV5.5.vue'), meta: { title: "编译计划5.5" } },
    { path: "/plan/boss5.6", component: () => import('@/views/execute/game/BossGameV5.6.vue'), meta: { title: "编译计划5.6" } },
    { path: "/plan/boss5.7", component: () => import('@/views/execute/game/BossGameV5.7.vue'), meta: { title: "编译计划5.7" } },
    { path: "/plan/boss5.7.1", component: () => import('@/views/execute/game/BossGameV5.7.1.vue'), meta: { title: "编译计划5.7.1" } },
    { path: "/plan/boss5.8", component: () => import('@/views/execute/game/BossGameV5.8.vue'), meta: { title: "编译计划5.8" } },
    { path: "/plan/boss5.9", component: () => import('@/views/execute/game/BossGameV5.9.vue'), meta: { title: "编译计划5.9" } },
    { path: "/plan/boss6.0", component: () => import('@/views/execute/game/BossGameV6.vue'), meta: { title: "编译计划6.0" } },
    { path: "/plan/wisdom2.0", component: () => import('@/views/execute/game/Wisdomplusv20.vue'), meta: { title: "AI计划2.0" } },
    { path: "/plan/wisdom2.1", component: () => import('@/views/execute/game/Wisdomplusv21.vue'), meta: { title: "AI计划2.1" } },
    { path: "/plan/wisdom2.3", component: () => import('@/views/execute/game/Wisdomplusv23.vue'), meta: { title: "AI计划2.3" } },
    { path: "/plan/wisdom2.3.1", component: () => import('@/views/execute/game/Wisdomplusv23_1.vue'), meta: { title: "AI计划2.3.1" } },
    { path: "/plan/wisdom2.5", component: () => import('@/views/execute/game/Wisdomplusv25.vue'), meta: { title: "AI计划2.5" } },
    { path: "/plan/wisdom2.6", component: () => import('@/views/execute/game/Wisdomplusv26.vue'), meta: { title: "AI计划2.6" } },
    { path: "/plan/wisdom2.7", component: () => import('@/views/execute/game/Wisdomplusv27.vue'), meta: { title: "AI计划2.7" } },
    { path: "/plan/wisdom2.9", component: () => import('@/views/execute/game/Wisdomplusv29.vue'), meta: { title: "AI计划2.9" } },
    { path: "/plan/wisdom2.9.1", component: () => import('@/views/execute/game/Wisdomplusv29_1.vue'), meta: { title: "AI计划2.9.1" } },
    { path: "/plan/wisdom2.9.2", component: () => import('@/views/execute/game/Wisdomplusv29_2.vue'), meta: { title: "AI计划2.9.2" } },
    { path: "/plan/wisdom2.9.3", component: () => import('@/views/execute/game/Wisdomplusv29_3.vue'), meta: { title: "AI计划2.9.3" } },
    { path: "/plan/wisdom2.9.5", component: () => import('@/views/execute/game/Wisdomplusv29_5.vue'), meta: { title: "AI计划2.9.5" } },
    { path: "/plan/wisdom2.9.6", component: () => import('@/views/execute/game/Wisdomplusv29_6.vue'), meta: { title: "AI计划2.9.6" } },
    { path: "/plan/wisdom3.0", component: () => import('@/views/execute/game/Wisdomplusv30.vue'), meta: { title: "AI计划3.0" } },
    { path: "/plan/wisdom3.1", component: () => import('@/views/execute/game/Wisdomplusv3_1.vue'), meta: { title: "AI计划3.1" } },
    { path: "/plan/wisdom3.2", component: () => import('@/views/execute/game/Wisdomplusv3_2.vue'), meta: { title: "AI计划3.2" } },
    { path: "/plan/wisdom3.3", component: () => import('@/views/execute/game/Wisdomplusv3_3.vue'), meta: { title: "AI计划3.3" } },
    { path: "/plan/wisdom3.5", component: () => import('@/views/execute/game/Wisdomplusv3_5.vue'), meta: { title: "AI计划3.5" } },
    { path: "/plan/wisdom3.6", component: () => import('@/views/execute/game/Wisdomplusv3_6.vue'), meta: { title: "AI计划3.6" } },
    { path: "/plan/wisdom3.7", component: () => import('@/views/execute/game/Wisdomplusv3_7.vue'), meta: { title: "AI计划3.7" } },
    { path: "/plan/wisdom3.8", component: () => import('@/views/execute/game/Wisdomplusv3_8.vue'), meta: { title: "AI计划3.8" } },
    { path: "/plan/wisdom4.0", component: () => import('@/views/execute/game/Wisdomplusv40.vue'), meta: { title: "AI计划4.0" } },
    { path: "/plan/wisdom9.0", component: () => import('@/views/execute/game/Wisdomplusv90.vue'), meta: { title: "AI计划9.0" } },
    { path: "/plan/wisdom9.1", component: () => import('@/views/execute/game/Wisdomplusv91.vue'), meta: { title: "AI计划9.1" } },
    { path: "/plan/wisdom9.2", component: () => import('@/views/execute/game/Wisdomplusv92.vue'), meta: { title: "AI计划9.2" } },
    { path: "/plan/wisdom9.3", component: () => import('@/views/execute/game/Wisdomplusv93.vue'), meta: { title: "AI计划9.3" } },
    { path: "/plan/wisdom9.5", component: () => import('@/views/execute/game/Wisdomplusv95.vue'), meta: { title: "AI计划9.5" } },
    { path: "/plan/wisdom9.6", component: () => import('@/views/execute/game/Wisdomplusv96.vue'), meta: { title: "AI计划9.6" } },
    { path: "/plan/wisdom9.8", component: () => import('@/views/execute/game/Wisdomplusv98.vue'), meta: { title: "AI计划9.8" } },
    { path: "/plan/wisdom9.9", component: () => import('@/views/execute/game/Wisdomplusv99.vue'), meta: { title: "AI计划9.9" } },
    { path: "/plan/wisdom9.9.1", component: () => import('@/views/execute/game/Wisdomplusv99V1.vue'), meta: { title: "AI计划9.9.1" } },
    { path: "/plan/wisdom9.9.2", component: () => import('@/views/execute/game/Wisdomplusv99V2.vue'), meta: { title: "AI计划9.9.2" } },
    { path: "/ttt/getstorage", component: () => import('@/views/GetStorerage.vue'), meta: { title: "获取配置" } },
    { path: "/ttt/setstorage", component: () => import('@/views/SetStorerage.vue'), meta: { title: "更改配置" } },
];

const router = createRouter({
    history: createWebHashHistory(),
    routes
});


router.beforeEach((to, _from, next) => {
    if(import.meta.env?.DEV){
        next();
        return;
    }
    document.title = (to.meta.title as string);
    if (!to.meta.anonymous) {
        let url = to.path;
        let token = storage.getStorage(AppConsts.USER_TKN);
        if (token) {
            if(to.path.startsWith("/plan/"))
            {
                checkUserProduct(url).then(()=>{
                    next();
                 }).catch((resp)=>{
                    showToast("无权访问");
                    setTimeout(()=>{
                        router.back();
                    },1000)
                    
                 })
            }
             else{
                next();
             }
             
        } else {
            console.log(url);
            router.replace({ path: "/login", query: { retUrl: url } });
        }
    } else {
        next();
    }
});

export default router;